package cn.swust.service.serviceimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.swust.model.UserBean;
import cn.swust.service.UserService;
import cn.swust.util.DBUtil;

public class UserServiceImpl implements UserService{

	public int addUser(UserBean user) {
		int ret = 0;
		try{
		String insertSql = "insert into user(user_name,user_password,user_flag,user_gold,user_lovegold" +
		",user_chname,user_idcard,user_email,user_sex,user_class,user_tel,user_payPwd) values ('" + user.getName() +
		"','" + user.getPassword() + "'," + user.getFlag() + "," + user.getGold() +
		"," + user.getLoveGold() + ",'" + user.getChName() + "','" + user.getIdCard() + "','" +
		user.geteMail() +  "'," + user.getSex() + "," + user.getClassify() +",'" + user.getTel() + "','"
		+ user.getPayPwd() + "')";
		Connection conn = DBUtil.getConnection();
		Statement state = conn.createStatement();
		state.execute(insertSql, Statement.RETURN_GENERATED_KEYS);
		ResultSet rs = state.getGeneratedKeys();
		while(rs.next())
			ret = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return ret;
	}

	public int delUserById(int id) {
		String delSql = "delete from user where user_id = " + id;
		return DBUtil.updateSql(delSql);
	}

	public int delUserByName(String name) {
		String delSql = "delete from user where user_name = '" + name  + "'";
		return DBUtil.updateSql(delSql);
	}

	public int modUser(int id, UserBean user) {
		String modSql = "update user set user_name = '" + user.getName() + "',user_password = '" +
		user.getPassword() + "',user_flag = " + user.getFlag() + ",user_gold = " + user.getGold() +
		",user_lovegold = " + user.getLoveGold() + ",user_chname = '" + user.getChName() + 
		"',user_idcard = '" + user.getIdCard() + "',user_email = '" + user.geteMail() +
		"',user_sex = " + user.getSex() + ",user_class = " + user.getClassify() +
		",user_tel = '" + user.getTel() + "' where user_id = " + id;
		return DBUtil.updateSql(modSql);
	}

	public UserBean queryById(int id) {
		String insertSql = "select * from user where user_id = " + id;
		ResultSet rs = DBUtil.querySql(insertSql, DBUtil.creatStatement(DBUtil.getConnection()));
		UserBean user = new UserBean();
		try {
			if (rs.next()) {
				user.setId(rs.getInt("user_id"));
				user.setName(rs.getString("user_name"));
				user.setPassword(rs.getString("user_password"));
				user.setFlag(rs.getInt("user_flag"));
				user.setGold(rs.getFloat("user_gold"));
				user.setLoveGold(rs.getFloat("user_lovegold"));
				user.setChName(rs.getString("user_chname"));
				user.setIdCard(rs.getString("user_idcard"));
				user.seteMail(rs.getString("user_email"));
				user.setSex(rs.getInt("user_sex"));
				user.setClassify(rs.getInt("user_class"));
				user.setTel(rs.getString("user_tel"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	public UserBean queryByName(String name) {
		String insertSql = "select * from user where user_name = '" + name + "'";
		ResultSet rs = DBUtil.querySql(insertSql, DBUtil.creatStatement(DBUtil.getConnection()));
		UserBean user = new UserBean();
		try {
			if (rs.next()) {
				user.setId(rs.getInt("user_id"));
				user.setName(rs.getString("user_name"));
				user.setPassword(rs.getString("user_password"));
				user.setFlag(rs.getInt("user_flag"));
				user.setGold(rs.getFloat("user_gold"));
				user.setLoveGold(rs.getFloat("user_lovegold"));
				user.setChName(rs.getString("user_chname"));
				user.setIdCard(rs.getString("user_idcard"));
				user.seteMail(rs.getString("user_email"));
				user.setSex(rs.getInt("user_sex"));
				user.setClassify(rs.getInt("user_class"));
				user.setTel(rs.getString("user_tel"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	public int modPayPwd(String userName, String paypwd) {
		String modSql = "update user set user_payPwd = '" + paypwd
		+ "' where user_name = '" + userName + "'";
		return DBUtil.updateSql(modSql);
	}

}
